package com.kgc.scd.controller;

import com.alibaba.fastjson.JSON;
import com.kgc.scd.bean.Product;
import com.kgc.scd.bean.User;
import com.kgc.scd.service.GuessUserLikeService;
import com.kgc.scd.util.RedisUtils;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import com.kgc.scd.util.UserAnno;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created On : 2022/6/13.
 * <p>
 * Author     : 98730
 * <p>
 * Description: 猜你喜欢操作入口
 */
@Slf4j
@RestController
public class GuessUserLikeController {

    @Autowired
    private GuessUserLikeService guessUserLikeService;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * @author : 98730
     * @date   : 2022/6/13
     * @param  : [com.kgc.scd.util.UserAnno]
     * @return : com.kgc.scd.util.RequestResult<java.util.List<com.kgc.scd.bean.Product>>
     * @description : 猜你喜欢
     */
    @PostMapping("/guessYouLike")
    public RequestResult<List<Product>> guessYouLike(@RequestParam String token){
        // 没有登录
        if (token.equals("no")){

            List<Product> allProduct = guessUserLikeService.queryAllProduct();
            List<Product> guessList = new ArrayList<>();

            Random random = new Random();
            List<Integer> randomList = new ArrayList<>();
            // 生成五个不同的随机数
            while (randomList.size() != 5) {
                Integer i = random.nextInt(allProduct.size());
                if (!randomList.contains(i)) {
                    randomList.add(i);
                }
            }
            for (Integer i : randomList) {
                guessList.add(allProduct.get(i));
            }
            return ResultBuildUtil.success(guessList);

        }

        // 已登录
        Object o = redisUtils.get(token);
        UserAnno userAnno = JSON.parseObject(o.toString(), UserAnno.class);

        // 查询用户喜欢列表
        List<Product> productListByCollect = guessUserLikeService.queryCollectListByUser(userAnno.getUserId());
        log.info("server-productListByCollect：{}", productListByCollect);
        // 查询用户足迹列表
        List<Product> productListByFoot = guessUserLikeService.queryFootListByUser(userAnno.getUserId());
        log.info("server-productListByFoot：{}", productListByFoot);
        List<Product> allProduct = guessUserLikeService.queryAllProduct();
        // 如果数量小于5，展示全部商品
        if (productListByCollect.size() != 0) {
            if (productListByCollect.size() < 5) {
                return ResultBuildUtil.success(productListByCollect);
            }

            List<Product> guessList = new ArrayList<>();

            Random random = new Random();
            List<Integer> randomList = new ArrayList<>();
            // 生成五个不同的随机数
            while (randomList.size() != 5) {
                Integer i = random.nextInt(productListByCollect.size());
                if (!randomList.contains(i)) {
                    randomList.add(i);
                }
            }
            for (Integer i : randomList) {
                guessList.add(productListByCollect.get(i));
            }
            return ResultBuildUtil.success(guessList);
        } else if (productListByFoot.size() != 0){
            if (productListByFoot.size() < 5) {
                log.info("productListByFoot----：{}",productListByFoot);
                return ResultBuildUtil.success(productListByFoot);
            }

            List<Product> guessList = new ArrayList<>();

            Random random = new Random();
            List<Integer> randomList = new ArrayList<>();
            // 生成五个不同的随机数
            while (randomList.size() != 5) {
                Integer i = random.nextInt(productListByFoot.size());
                if (!randomList.contains(i)) {
                    randomList.add(i);
                }
            }
            for (Integer i : randomList) {
                guessList.add(productListByFoot.get(i));
            }
            return ResultBuildUtil.success(guessList);
        }

        List<Product> guessList = new ArrayList<>();

        Random random = new Random();
        List<Integer> randomList = new ArrayList<>();
        // 生成五个不同的随机数
        while (randomList.size() != 5) {
            Integer i = random.nextInt(allProduct.size());
            if (!randomList.contains(i)) {
                randomList.add(i);
            }
        }
        for (Integer i : randomList) {
            guessList.add(allProduct.get(i));
        }
        return ResultBuildUtil.success(guessList);

    }
}
